package com.driving.single.frontend.school.service.impl;

import com.driving.single.common.domain.TbSchool;
import com.driving.single.common.domain.TbSchoolClasses;
import com.driving.single.common.domain.TbSchoolService;
import com.driving.single.common.entity.DrivingResult;
import com.driving.single.common.entity.PageResult;
import com.driving.single.common.redis.service.CommonRedisSchoolClassesService;
import com.driving.single.common.redis.service.CommonRedisSchoolService;
import com.driving.single.common.redis.service.CommonRedisSchoolServiceService;
import com.driving.single.common.school.service.CommonSchoolClassesService;
import com.driving.single.common.school.service.CommonSchoolCommentService;
import com.driving.single.common.school.service.CommonSchoolPhotoService;
import com.driving.single.common.school.service.CommonSchoolService;
import com.driving.single.common.school.service.CommonSchoolServiceService;
import com.driving.single.frontend.school.service.FrontendSchoolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FrontendSchoolServiceImpl implements FrontendSchoolService {
    @Autowired
    private CommonSchoolService commonSchoolService; // 驾校接口
    @Autowired
    private CommonRedisSchoolService commonRedisSchoolService; // 驾校Redis接口
    @Autowired
    private CommonSchoolClassesService commonSchoolClassesService; // 驾校班级接口
    @Autowired
    private CommonRedisSchoolClassesService commonRedisSchoolClassesService; // 驾校班级Redis接口
    @Autowired
    private CommonSchoolServiceService commonSchoolServiceService; // 驾校服务接口
    @Autowired
    private CommonRedisSchoolServiceService commonRedisSchoolServiceService; // 驾校服务Redis接口
    @Autowired
    private CommonSchoolCommentService commonSchoolCommentService; // 驾校评论接口
    @Autowired
    private CommonSchoolPhotoService commonSchoolPhotoService; // 驾校相册接口

    /**
     * 实现根据主键查询驾校接口
     *
     * @param id 主键
     */
    @Override
    public DrivingResult selectSchoolById(Long id) {
        // 1.查询Redis缓存
        try {
            TbSchool redisTbSchool = this.commonRedisSchoolService.selectRedisTbSchoolById(id);
            if (redisTbSchool != null) {
                return DrivingResult.ok(redisTbSchool);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 2.查询数据库
        TbSchool tbSchool = this.commonSchoolService.selectTbSchoolById(id);
        if (tbSchool == null) {
            return DrivingResult.error("查无数据");
        }
        // 3.插入Redis缓存
        try {
            this.commonRedisSchoolService.insertRedisSchool(tbSchool);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return DrivingResult.ok(tbSchool);
    }

    /**
     * 实现根据驾校ID  查询驾校班级及费用  接口
     * @param school_id 主键
     */
    @Override
    public DrivingResult selectSchoolClassesBySid(Long school_id) {
        // 1.查询Redis缓存
        try {
            List<Map<String, Object>> redisList =
                    this.commonRedisSchoolClassesService.selectRedisTbSchoolClassesBySid(school_id);
            if (redisList != null && redisList.size() > 0) {
                return DrivingResult.ok(redisList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 2.查询数据库
        List<TbSchoolClasses> list =
                this.commonSchoolClassesService.selectSchoolClassesBySchoolId(school_id);
        if (list == null || list.size() == 0) {
            return DrivingResult.error("查无数据");
        }
        // 3.数据加工
        List<Map<String, Object>> tbSchoolClassesMap = new ArrayList<>();
        Map<String, Object> map = null;
        for (TbSchoolClasses tbSchoolClasses : list) {
            map = new HashMap<>();
            map.put("className",tbSchoolClasses.getName());
            map.put("licenseType",Arrays.asList(tbSchoolClasses.getLicenseType().split(",")));
            map.put("carDistr",Arrays.asList(tbSchoolClasses.getCarDistr().split(",")));
            map.put("passTime",tbSchoolClasses.getPassTime());
            map.put("pickWay",tbSchoolClasses.getPickWay());
            map.put("alonePrice",tbSchoolClasses.getAlonePrice());
            map.put("teamPrice",tbSchoolClasses.getTeamPrice());
            tbSchoolClassesMap.add(map);
        }
        // 4.插入Redis缓存
        try {
            this.commonRedisSchoolClassesService.insertRedisSchoolClasses(tbSchoolClassesMap, school_id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return DrivingResult.ok(tbSchoolClassesMap);
    }

    /**
     * 实现根据驾校ID  查询驾校服务   接口
     *
     * @param school_id 主键
     */
    @Override
    public DrivingResult selectSchoolServiceBySid(Long school_id) {
        // 1.查询Redis缓存
        try {
            Map<String, Object> redisTbSchoolServiceMap =
                    this.commonRedisSchoolServiceService.selectRedisTbSchoolServiceById(school_id);
            if (redisTbSchoolServiceMap != null) {
                return DrivingResult.ok(redisTbSchoolServiceMap);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 2.查询数据库
        TbSchoolService tbSchoolService = this.commonSchoolServiceService.selectSchoolServiceBySid(school_id);
        if (tbSchoolService == null) {
            return DrivingResult.error("查无数据");
        }
        // 3.数据加工
        Map<String, Object> tbSchoolServiceMap = new HashMap<>();
        try {
            tbSchoolServiceMap.put("scale", tbSchoolService.getScale());
            tbSchoolServiceMap.put("introduction", tbSchoolService.getIntroduction());
            tbSchoolServiceMap.put("service", new ArrayList<>(Arrays.asList(tbSchoolService.getService().split(","))));
            tbSchoolServiceMap.put("found_time",
                    new SimpleDateFormat("yyyy-MM-dd").format(tbSchoolService.getFoundTime()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 4.插入Redis缓存
        try {
            this.commonRedisSchoolServiceService.insertRedisTbSchoolService(tbSchoolServiceMap, school_id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return DrivingResult.ok(tbSchoolServiceMap);
    }

    /**
     * 实现 找驾校页面驾校查询 接口
     *
     * @param page        当前页
     * @param rows        页显示数
     * @param area        查询条件：区域(学校名称)
     * @param licenseType 查询条件：驾照类型
     * @param minPrice    查询条件：最小价格
     * @param maxPrice    查询条件：最大价格
     */
    @Override
    public DrivingResult selectTbSchoolListByConditionWithPage(Integer page, Integer rows, String area,
                                                               String licenseType,
                                                               Integer minPrice, Integer maxPrice) {
        // 1.价格输入校验
        if (maxPrice != null && minPrice != null && maxPrice < minPrice) {
            return DrivingResult.error("价格区间有误，请核实输入！");
        }
        // 2.分页查询数据库
        PageResult pageResult = this.commonSchoolService.selectTbSchoolListByConditionWithPage(page, rows, area,
                licenseType, minPrice, maxPrice);
        if (pageResult == null || pageResult.getResult().size() == 0) {
            return DrivingResult.error("查无数据");
        }
        return DrivingResult.ok(pageResult);
    }

    /**
     * 实现根据驾校ID   查询驾校评论   接口
     *
     * @param school_id 驾校主键
     * @param page      当前页
     * @param rows      页显示数
     */
    @Override
    public DrivingResult selectSchoolCommentBySidWithPage(Long school_id, Integer page, Integer rows) {
        PageResult pageResult = this.commonSchoolCommentService.selectTbSchoolCommentListBySidWithPage(school_id,
                page, rows);
        if (pageResult == null || pageResult.getResult().size() == 0) {
            return DrivingResult.error("查无数据");
        }
        return DrivingResult.ok(pageResult);
    }

    /**
     * 实现根据驾校ID   分页查询驾校相册   接口
     *
     * @param school_id 驾校主键
     * @param photoType 相册图片类型,1:训练场地,2:训练车,3:周边环境
     * @param page      当前页
     * @param rows      页显示数
     */
    @Override
    public DrivingResult selectSchoolPhotoAlbumBySidWithPage(Long school_id, Integer photoType, Integer page,
                                                             Integer rows) {
        PageResult pageResult = this.commonSchoolPhotoService.selectSchoolPhotoAlbumBySidWithPage(school_id,
                photoType, page, rows);
        if (pageResult == null || pageResult.getResult().size() == 0) {
            return DrivingResult.error("查无数据");
        }
        return DrivingResult.ok(pageResult);
    }
}
